package com.jic.order.controller.manage;

import com.jic.common.base.vo.RestResult;
import com.jic.order.request.OrderRouteSplitWarehouseConditionRequest;
import com.jic.order.response.OrderRouteSplitWarehouseConditionResponse;
import com.jic.order.service.OrderRouteSplitWarehouseConditionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = "路由分仓规则匹配条件管理")
@RequestMapping(value = "orderRouteSplitWarehouseCondition")
@RestController
public class OrderRouteSplitWarehouseConditionController {

    @Autowired
    private OrderRouteSplitWarehouseConditionService orderRouteSplitWarehouseConditionService;

    @ApiOperation(value = "查询列表")
    @PostMapping(value = "/queryConditionListOther")
    public RestResult<List<OrderRouteSplitWarehouseConditionResponse>> queryMerchantListOther(@RequestBody OrderRouteSplitWarehouseConditionRequest form) {
        List<OrderRouteSplitWarehouseConditionResponse> OrderRouteSplitWarehouseConditions = orderRouteSplitWarehouseConditionService.query(form);
        return RestResult.success(OrderRouteSplitWarehouseConditions);
    }

    @ApiOperation(value = "删除规则")
    @PostMapping(value = "/deleteCondition")
    public RestResult deleteCondition(@RequestBody OrderRouteSplitWarehouseConditionRequest form) {
        RestResult restResult = new RestResult<>();
        try {
            long flag = orderRouteSplitWarehouseConditionService.delete(form.getId());
            if (flag == 1) {
                restResult = RestResult.success(restResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
            restResult = RestResult.error("9999", e.getLocalizedMessage());
        }
        return restResult;

    }

    @ApiOperation(value = "路由分仓规则匹配条件编辑/新增")
    @PostMapping(value = "editorderRouteSplitWarehouseCondition")
    public RestResult<Long> editorderRouteSplitWarehouseCondition(@RequestBody OrderRouteSplitWarehouseConditionRequest form) {
        long result = 0;
        //修改
        if (form.getId() > 0) {
            result = orderRouteSplitWarehouseConditionService.update(form);
        } else {//新增
            form.setCreator(1);
            form.setUpdater(1);
            result = orderRouteSplitWarehouseConditionService.insert(form);
        }
        return RestResult.success(result);
    }
}
